package minecraft.xiyu.exchantedbook.procedures;

import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.Entity;

import minecraft.xiyu.exchantedbook.init.ExchantedBookModEnchantments;

public class AttackspeedlowProcedure {
	public static void execute(Entity entity) {
		if (entity instanceof LivingEntity livingEntity && !entity.level.isClientSide) {
			int attackSpeedSlowEnchantmentLevel = EnchantmentHelper.getItemEnchantmentLevel(ExchantedBookModEnchantments.ATTACKSPEEDLOW.get(), livingEntity.getMainHandItem());

			AttributeInstance attackSpeedAttribute = livingEntity.getAttribute(Attributes.ATTACK_SPEED);
			double baseAttackSpeed = 4.0; // 默认攻击速度

			if (attackSpeedSlowEnchantmentLevel > 0) {
				// 每级附魔降低一定的攻击速度
				double reductionPerLevel = 0.2; // 每级降低0.2的攻击速度
				double totalReduction = attackSpeedSlowEnchantmentLevel * reductionPerLevel;
				double newAttackSpeed = baseAttackSpeed - totalReduction;

				// 确保攻击速度不会低于最小值
				double minAttackSpeed = 2.0; // 最小攻击速度
				newAttackSpeed = Math.max(newAttackSpeed, minAttackSpeed);

				attackSpeedAttribute.setBaseValue(newAttackSpeed);
			}
		}
	}
}